const Service = require("egg").Service;

class UserInfo extends Service {
  // 添加用户地址
  async addUserInfo() {
    let { address, phone, name, user_id } = this.ctx.request.body;

    let obj = { address, phone, name, user_id };

    let res = await this.app.model.UserInfo.create(obj);

    if (!res)
      return {
        msg: "添加失败"
      };

    return {
      msg: "添加成功"
    };
  }

  // 修改用户地址
  async updateUserInfo() {
    let id = this.ctx.params.id;

    let { address, phone, name, user_id } = this.ctx.request.body;

    let obj = { address, phone, name, user_id };

    let res = await this.app.model.UserInfo.update(obj, {
      where: {
        id
      }
    });

    if (!res) return { msg: "修改失败" };

    return {
      msg: "修改成功"
    };
  }

  // 删除商品
  async deleteUserInfo() {
    let id = this.ctx.params.id;
    let res = await this.app.model.UserInfo.destroy({ where: { id } });
    if (!res) return { msg: "删除失败" };

    return {
      msg: "删除成功"
    };
  }

  // 根据用户id查询用户地址信息列表
  async queryUserInfoById() {
    // let user_id = this.ctx.request.body.user_id;

    let url = this.ctx.request.url;

    let user_id = url.match(/=(\S*)/)[1];

    let res = await this.app.model.UserInfo.findAll({
      where: {
        user_id
      }
    });

    if (!res) return { msg: "查询失败" };

    return  res
    
  }
}

module.exports = UserInfo;
